package com.example.farmeri.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.farmeri.R;
import com.example.farmeri.ui.farm_data.FarmDataFragment;
import com.example.farmeri.ui.mine.MineFragment;
import com.example.farmeri.ui.news.NewsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.main_pager)
    ViewPager pager;
    @BindView(R.id.main_noticeBtn)
    RadioButton noticeBtn;
    @BindView(R.id.main_farmBtn)
    RadioButton farmBtn;
    @BindView(R.id.main_mineBtn)
    RadioButton mineBtn;
    @BindView(R.id.main_radioGroup)
    RadioGroup radioGroup;

    List<Fragment> fragmentList;
    MyPagerAdapter myPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        addFragment();
        initPager();
        //
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.main_noticeBtn:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.main_farmBtn:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.main_mineBtn:
                        pager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
            }
        });

        //设置切换监听事件
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void initPager() {
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(myPagerAdapter);
    }

    private void addFragment() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(NewsFragment.newInstance());
        fragmentList.add(FarmDataFragment.newInstance());
        fragmentList.add(MineFragment.newInstance());
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pager = null;
        fragmentList = null;
    }


}
